package com.mbuswidget;

import java.util.ArrayList;
import java.util.Collections;

import org.w3c.dom.Element;

import android.location.Location;

public class BusStop {
	private final String uniquename;
	private String shortName;
	private final ArrayList<Double> timeToArrival;
	private final Integer numOfTOA;
	private final Location location;
	
	public BusStop(BusStop parent) {
		uniquename = new String(parent.uniquename);
		shortName = new String(parent.shortName);
		timeToArrival = new ArrayList<Double>(parent.timeToArrival);
		numOfTOA = parent.numOfTOA;
		if(parent.location != null) {
			location = new Location(parent.location);
		} else {
			location = null;
		}
	}
	
	public BusStop(Element stopXMLElment) {
		uniquename = Utils.getTextValue(stopXMLElment, "name");
		shortName =Utils.getTextValue(stopXMLElment, "name2");
		numOfTOA = Integer.parseInt(Utils.getTextValue(stopXMLElment, "toacount"));
		timeToArrival = new ArrayList<Double>();
		double latitude = Double.parseDouble(Utils.getTextValue(stopXMLElment, "latitude"));
		double longitude = Double.parseDouble(Utils.getTextValue(stopXMLElment, "longitude"));
		
		for (int i = 1; i <= numOfTOA; i++) {
			String toaTag = "toa" + i;
			String toaString = Utils.getTextValue(stopXMLElment, toaTag);
			Double toa = Double.parseDouble(toaString);
			timeToArrival.add(toa);
		}
		
		Collections.sort(timeToArrival);

		location = new Location("mbus");
		location.setLatitude(latitude);
		location.setLongitude(longitude);
	}
	
	public String getLongName() {
		return uniquename;
	}
	public String getShortName() {
		return shortName;
	}
	
	public void setShortName(String s) {
		shortName = s;
	}
	
	public String getAbrivatedName() {
		// TODO: FIX SHORT NAME
		int count = 0;
		String name = new String();
		for (String word : shortName.split("\\s+")) {
			if(word.length() != 0) {
				name += Character.toUpperCase(word.charAt(0));
			}
			if(++count > 3) {
				break;
			}
		}
		return name;
	}
	
	public ArrayList<Double> getArrivalTimes() {
		return timeToArrival;
	}
	
	public Location getLocation(){
		return new Location(location);
	}
}